import {isFunc, isNumber} from './type'
const tagsViewWhitelist = ['/login', '/404', '/403', '/500', '502']

// 欢迎文本
export const generateWelcomeMsg = () =>{
  let timeNow = new Date().getHours()
  let text = ``
  // 判断当前时间段
  if (timeNow >= 0 && timeNow <= 10) text = `早上好`
  else if (timeNow > 10 && timeNow <= 14) text = `中午好`
  else if (timeNow > 14 && timeNow <= 18) text = `下午好`
  else if (timeNow > 18 && timeNow <= 24) text = `晚上好`
  return text
}

// 获取函数参数名称
export const getArgsName = fn => {
  if (!isFunc(fn)) {
    throw new Error(`param must be a function, not ${typeof fn}`)
  }
  // 匹配函数括弧里的参数
  const args = fn.toString().match(/function\s.*?\(([^)]*)\)/)[1]

  // 分解参数成数组, 去除空格和内联注释, 确保没有undefineds
  return args.split(',').map(arg => arg.replace(/\/\*.*\*\//, '').trim()).filter(args => args)
}

// 随机字符串
export const getRandomStr = () => new Date().getTime() + Math.random().toString(16).slice(2)

// 千分符
export const groupSeparator= num => {
  if (!isNumber(num)) {
    throw new Error(`params must be number, not ${typeof obj}`)
  }
	num = num + ''
	if (!num.includes('.')) {
		num += '.'
	}
	return num.replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
		return $1 + ','
	}).replace(/\.$/, '')
}

// 过滤不需要添加的tag，如：登录、404
export const hasAddTag = path => !tagsViewWhitelist.includes(path)